package com.vote.www.service;

import com.alibaba.fastjson.JSONObject;
import com.vote.www.core.page.Pagination;
import com.vote.www.domain.Czjdataresource;
import com.vote.www.domain.Czjuserinfo;
import com.vote.www.domain.Czjusermess;
import com.vote.www.domain.Czjvote;
import com.vote.www.persistence.SpokesmanMapper;
import com.vote.www.utils.ConvertObj;
import com.vote.www.utils.MapManage;
import com.vote.www.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import tangwj.person.dao.ResourceDao;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by 汤伟杰 on 2017/4/18.
 */
@Service
public class SpokesmanService {
    @Autowired
    private ResourceDao resourceDao;
    @Autowired
    private SpokesmanMapper spokesmanMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DataresourceService dataresourceService;

    public Object getIndex(HttpServletRequest req) throws Exception {
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
//        Pagination pagination = findList(1,pageNo, pageSize);
//        Pagination pagination2 = findList(2,pageNo, pageSize);
//        Pagination pagination3 = findList(3,pageNo, pageSize);

        List<Czjuserinfo> czjuserinfos = new ArrayList<Czjuserinfo>();
        for (int i = 0; i < 1000; i++) {
            Czjuserinfo czjuserinfo = new Czjuserinfo();
            czjuserinfo.setNickname("用户"+ UUIDUtils.getUUID());
            czjuserinfo.setType(0);
            czjuserinfo.setIsdelete("0");
            czjuserinfo.setEnable("Y");
            czjuserinfo.setTelephone("13539925170"+i);
            czjuserinfo.setPassword("123456");
            czjuserinfo.setCarplate("carplate");
            czjuserinfo.setRandom("随机数");
            czjuserinfo.setCreatetime(new Date());
            czjuserinfo.setUpdatetime(new Date());
            czjuserinfo.setUpdateuser("当前用户");
            czjuserinfos.add(czjuserinfo);

        }
        resourceDao.batchInsert(czjuserinfos);

        List<Czjusermess> czjusermesss = new ArrayList<Czjusermess>();
        int index = 18;
        for (int i = 1000-1; i >=0; i--) {
            Czjusermess czjusermess = new Czjusermess();
            czjusermess.setUsermessId(UUIDUtils.getUUID());
            czjusermess.setSex(1);
            czjusermess.setNick("昵称"+i);
            czjusermess.setFullName("全民");
            czjusermess.setUserinfoId(index+i+1);
            czjusermess.setFans(200);
            czjusermess.setRepresent(i%3+1);
            czjusermess.setIrectSeeding(i%2);
            czjusermess.setPicPath("/upload/user/"+i+".jpg");
            czjusermess.setLocus("广州");
            czjusermess.setIdCard("440782199XXX"+i);
            czjusermess.setUusee("优酷");
            czjusermess.setUserinfoId(i+1);
            czjusermesss.add(czjusermess);

        }
        resourceDao.batchInsert(czjusermesss);
        for (int j = 0; j < 5; j++) {
            List<Czjvote> czjvotes = new ArrayList<Czjvote>();
            for (int i = 0; i < 10000; i++) {
                Czjvote czjvote = new Czjvote();
                czjvote.setCreatetime(new Date());
                czjvote.setUserinfoId(getRandom());
                czjvote.setVoteId(UUIDUtils.getUUID());
                czjvote.setPid("SID:1234567890");
                czjvotes.add(czjvote);
            }
            resourceDao.batchInsert(czjvotes);
        }


        System.out.println("成功");

        return "";
    }

    public Pagination findList(Integer type,String pageNo, String pageSize) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("represent", type);
        Pagination pagination = new Pagination();
        if(StringUtils.isNotBlank(pageNo)){
            pagination.setPageNo(Integer.parseInt(pageNo));
        }
        if(StringUtils.isNotBlank(pageSize)){
            pagination.setPageSize(Integer.parseInt(pageSize));
        }
        param.putAll(pagination.toMap());
        int count = spokesmanMapper.getByTypeCount(param);
        List<Map<String, Object>> list = spokesmanMapper.getByType(param);
        pagination.setList(list);
        pagination.setTotalCount(count);
        return pagination;
    }

    public int getRandom() {
        int max=1000;
        int min=0;
        Random random = new Random();
        int s = random.nextInt(max)%(max-min+1) + min;
        return s;
    }

    public Map<String,Object> findListMap(int i, String pageNo, String pageSize) throws Exception {
        //设置显示顺序
        Integer no = null;
        String[] chuxing ={"red","green","orange"};
//        String[] chuxingStr ={"红色出行","绿色出行","橙色出行"};
        List<Map<String, Object>> listmap1 = jdbcTemplate.queryForList("select * from czj_dataresource where code='SORT.CXSX'");
        for (Map<String, Object> map : listmap1) {
            if(chuxing[i-1].equals(String.valueOf(map.get("name")))){
                no = Integer.valueOf(String.valueOf(map.get("value")));
            }
        }

        Pagination pagination = findList(i, pageNo, pageSize);
        List<Map<String,Object>> list = (List<Map<String, Object>>) pagination.getList();
        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        Map<String,Object> mapobj = null;
        for (Map<String, Object> map : list) {
            mapobj = new HashMap<String, Object>();
            mapobj.put("userType",map.get("represent"));
            mapobj.put("userIcon",map.get("picPath"));
            mapobj.put("userNum",map.get("count"));
            mapobj.put("userId",map.get("userinfoId"));
            mapobj.put("locus",map.get("locus"));
            mapobj.put("nick",map.get("nick"));
            listmap.add(mapobj);
        }
        pagination.setList(listmap);
//        Map<String,Object> map = new HashMap<String,Object>();
//        map.put("firstPage",pagination.f);
        String obj = JSONObject.toJSONString(pagination);
        Map<String,Object> map = (Map<String, Object>) JSONObject.parse(obj);
        int num = pagination.getTotalCount()-pagination.getList().size()-(pagination.getPageNo()-1)*pagination.getPageSize();
        //25-10=15
        //25-10-(2-1)*10=5
        //25-5-(3-1)*10=0
        if(num<=0){
            map.put("remain",0);//剩余显示人数
        }else{
            map.put("remain",num);//剩余显示人数
        }
        map.put("no", no);//显示顺序
        return map;
    }

    public List<Map<String,Object>> getOrderList() {
        List<Map<String, Object>> listmap = jdbcTemplate.queryForList("select * from czj_dataresource where code='CXSX'");//出行顺序
        Map<String, Object> mapobject = new HashMap<String, Object>();
        for (Map<String, Object> map : listmap) {
            mapobject.put(String.valueOf(map.get("name")), map.get("value"));
        }

        return null;
    }

    public Object getResult() {
        Map<String,Object> param = new HashMap<String,Object>();
        List<Map<String, Object>> listmap = spokesmanMapper.getByFirsttrip(param);

         for (int i = 0; i < listmap.size(); i++) {
             if(listmap.get(i).get("picPathPng")!=null&&StringUtils.isNotBlank(String.valueOf(listmap.get(i).get("picPathPng")))){
                 listmap.get(i).put("userIcon",listmap.get(i).get("picPathPng"));
             }else{
                 listmap.get(i).put("userIcon",listmap.get(i).get("picPath"));
             }
             listmap.get(i).put("userType",listmap.get(i).get("represent"));

             listmap.get(i).put("userNum",listmap.get(i).get("count"));
             listmap.get(i).put("userId",listmap.get(i).get("userinfoId"));
             listmap.get(i).put("locus",listmap.get(i).get("locus"));
             listmap.get(i).put("nick",listmap.get(i).get("nick"));
        }
        MapManage.returnReservedForListMap(listmap, "userIcon", "nick", "userType", "userNum", "userId","locus");


//        MapManage.reservedForListMap(listmap,"userIcon","nick","userType","userNum","userId");
        return listmap;
    }
}
